package com.framework.service.yhbservice.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.framework.dao.YHBDao.noticeDao.YhbNoticeDao;
import com.framework.page.PageUtils;
import com.framework.page.Query;
import com.framework.service.yhbservice.YhbNoticeService;
import com.framework.service.yhbservice.YhbUserService;
import com.framework.utils.string.StringUtil;
import com.framework.yhbEntity.notice.YhbNotice;
import com.framework.yhbEntity.user.YhbUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Transactional(rollbackFor = Exception.class)
@Service("yhbNoticeService")
public class YhbNoticeServiceImpl extends ServiceImpl<YhbNoticeDao,YhbNotice> implements YhbNoticeService {
    @Autowired
    private YhbUserService yhbUserService;
    @Override
    public List<YhbNotice> getIndexNotice(String token) {
        YhbUser user = yhbUserService.queryUserByToken(token);

        List<String> orderColumn=new ArrayList<>();
        orderColumn.add("create_date");
        List<YhbNotice> list =baseMapper.selectPage(new Page<YhbNotice>(1, 3), new EntityWrapper<YhbNotice>()
                .eq("dept_id", user.getDeptId())
                .orderAsc(orderColumn)
        );

        return list;
    }

    @Override
    public List<YhbNotice> getNoticeList(String token) {
        YhbUser user = yhbUserService.queryUserByToken(token);

        List<String> orderColumn=new ArrayList<>();
        orderColumn.add("create_date");
        List<YhbNotice> list =baseMapper.selectList(new EntityWrapper<YhbNotice>()
                .eq("dept_id", user.getDeptId())
                .orderDesc(orderColumn)
        );

        return list;

    }

    @Override
    public YhbNotice queryNoticeByNo(String token, String noticeNo) {
        YhbUser user = yhbUserService.queryUserByToken(token);
        YhbNotice n=new YhbNotice();
        n.setDeptId(user.getDeptId());
        n.setNoticeNo(noticeNo);
        return baseMapper.selectOne(n);

    }

    @Override
    public PageUtils queryNoticePage(Map<String, Object> params) {
        Long deptId= (Long) params.get("deptId");
        Page<YhbNotice> page = selectPage(new Query<YhbNotice>(params).getPage(), new EntityWrapper<YhbNotice>().eq("dept_id",deptId));
        return new PageUtils(page);
    }

    @Override
    public void insertNewNotice(String coverUrl, Long deptId, HttpServletRequest request) {
        YhbNotice sysNotice = new YhbNotice();
        String noticeNO= StringUtil.getDateString(7);
        noticeNO=verifyNoticeNo(noticeNO);
        String content=request.getParameter("content");
        String titles=request.getParameter("titles");
        String remark=request.getParameter("remark");
        sysNotice.setNoticeNo(noticeNO);
        sysNotice.setTitles(titles);
        sysNotice.setContent(content);
        sysNotice.setCreateDate(new Date());
        sysNotice.setRemark(remark);
        if(deptId.equals(2L)){//用户宝的固定地址
            sysNotice.setJumpUrl("https://kfs.yidaa.cn/index.php/Home/Indexuserp/xtgg/token");
        }
        sysNotice.setIsShow(1);
        sysNotice.setDeptId(deptId);
        sysNotice.setCoverImg(coverUrl);
        this.baseMapper.insert(sysNotice);

    }

    private String verifyNoticeNo(String noticeNO) {

        YhbNotice n = baseMapper.selectById(noticeNO);
        if(n!=null){
            noticeNO=StringUtil.getDateString(7);
            verifyNoticeNo(noticeNO);
        }
        return noticeNO;
    }


}
